package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.mystery;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.xxcxcxcx.xc_enhancedmob.component.BossBarComponent;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.damgesource.SkillDamageSource;
import net.xxcxcxcx.xc_enhancedmob.data.MobData;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.column.sever.SeverCustomBossBarColumn;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.CustomBossBar;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.sever.SeverCustomBossBar;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.type.reality.TestClientCustomBossBar;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.Ref;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

import java.util.UUID;

import static net.xxcxcxcx.xc_enhancedmob.packet.damage_num.DamageNumPacket.sendDamageNum;

public class ShieldSkill extends BaseSkill {
    public float ShieldCount;
    public float CumulativeDamage;
    public float MaxShieldCount;
    public Text ShieldValue = new LiteralText("护盾");
    public UUID ShieldUUID = UUID.fromString("4f9d5d80-2d29-7c2f-2a02-023790d90d43");
    public CustomBossBar bossBar = null;

    public ShieldSkill(int level) {
        super(level);
    }

    public ShieldSkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public ShieldSkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return "护盾技能，获得一个（10%+5%*level）*最大生命的护盾值，优先阻挡伤害，生命每累计下降（20%-level*1%）值恢复全部护盾，当5秒钟未受到伤害之后，护盾值以(4+level)/秒的速度回复，并同时减去生命损失累计值。";
    }

    @Override
    public SkillRarity getSkillRarity() {
        return SkillRarity.Mystery;
    }

    public NbtCompound getNbtCompound() {
        NbtCompound nbtCompound = super.getNbtCompound();
        nbtCompound.putFloat("ShieldCount", ShieldCount);
        nbtCompound.putFloat("CumulativeDamage", CumulativeDamage);
        nbtCompound.putFloat("MaxShieldCount", MaxShieldCount);
        return nbtCompound;
    }

    @Override
    public void readbyNbtCompound(NbtCompound nbtCompound) {
        super.readbyNbtCompound(nbtCompound);
        ShieldCount = nbtCompound.getFloat("ShieldCount");
        CumulativeDamage = nbtCompound.getFloat("CumulativeDamage");
        MaxShieldCount = nbtCompound.getFloat("MaxShieldCount");
    }

    @Override
    public int getEffectiveSequence() {
        return 100;
    }

    @Override
    public boolean onAttribute(MobEntity mobEntity) {
        MaxShieldCount = mobEntity.getMaxHealth() * (0.1f + 0.05f * getSkillLevel());
        ShieldCount = MaxShieldCount;
        CumulativeDamage = 0;
        if (((MobData) mobEntity).getComponent(BossBarComponent.class.getName()) instanceof BossBarComponent component) {
            if (component.bossBarComplete == null) {
                component.CreatbossBar();
            }
            if (component.bossBarComplete.getBossColumn(component.MainPart) instanceof SeverCustomBossBarColumn bossBarColumn) {
                bossBarColumn.addBossBar(new SeverCustomBossBar(
                        ShieldUUID,
                        ShieldValue,
                        new TestClientCustomBossBar(),
                        MaxShieldCount,
                        ShieldCount,
                        1, false, null, -1
                ));
            }
        }
        return true;
    }

    @Override
    public boolean onDamaged(LivingEntity attacker, MobEntity target, DamageSource source, Ref<Float> amount, boolean ismob, boolean remote) {
        if (source instanceof SkillDamageSource source1 && source1.getPassSkill()) return true;
        setSkilltime(target.world.getTime() + 100);
        if (ShieldCount > 0) {
            float injury = Math.min(amount.getValue(), ShieldCount);
            sendDamageNum(target, injury, source.name);
            amount.setValue(amount.getValue() - injury);
            ShieldCount -= injury;
        }
        if (amount.getValue() > 0) {
            CumulativeDamage += amount.getValue();
            if (CumulativeDamage >= target.getMaxHealth() * (0.2f - 0.01f * getSkillLevel())) {
                CumulativeDamage = 0;
                ShieldCount = MaxShieldCount;
            }
        }
        return true;
    }

    @Override
    public boolean passiveApply(MobEntity mobEntity) {
        if (ShieldCount < MaxShieldCount && mobEntity.world.getTime() > getSkilltime() && mobEntity.world.getTime() % 5 == 0) {
            float Updata = Math.min(1.0f + getSkillLevel() / 4.0f, MaxShieldCount - ShieldCount);
            ShieldCount += Updata;
            CumulativeDamage -= Updata;
            if (CumulativeDamage < 0) CumulativeDamage = 0;
        }
        if (mobEntity.isDead()) {
            ShieldCount = 0;
        }
        if (bossBar == null) {
            if (((MobData) mobEntity).getComponent(BossBarComponent.class.getName()) instanceof BossBarComponent component) {
                if (component.bossBarComplete != null) {
                    bossBar = component.bossBarComplete.getBossBar(ShieldUUID);
                }
            }
        }
        if (bossBar != null) {
            bossBar.setNowCount(ShieldCount);
            bossBar.setMaxCount(MaxShieldCount);
        }
        return true;
    }
}
